import { Component, OnInit, Input } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-changebelong-component',
  templateUrl: './changebelong-component.component.html',
  styleUrls: ['./changebelong-component.component.less']
})
export class ChangeBelongComponent implements OnInit {

  belongForm: FormGroup;

  constructor(private fb: FormBuilder) {
  }

  ngOnInit(): void {
    this.belongForm = this.fb.group({
      belongto: [null, [Validators.required]],
    });
  }

  submitForm(): void {
    for (const i in this.belongForm.controls) {
      this.belongForm.controls[i].markAsDirty();
      this.belongForm.controls[i].updateValueAndValidity();
    }
  }

  updateConfirmValidator(): void {
    /** wait for refresh value */
    Promise.resolve().then(() => this.belongForm.controls.checkPassword.updateValueAndValidity());
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.belongForm.controls.password.value) {
      return { confirm: true, error: true };
    }
  }

  getCaptcha(e: MouseEvent): void {
    e.preventDefault();
  }

}
